package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.adapter.TZPSListAdapter;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Check;
import com.dhcc.android.base.common.bean.Node;
import com.dhcc.android.base.db.DBImportUtil;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 样方图纸拍摄
 *
 * @author zhoudan
 * @date 2016年2月22日
 */
public class YFPhotoActivity extends SBaseActivity {

    private LinearLayout main;
    private ListView listview;
    private List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
    private TZPSListAdapter adapter;

    private SharedPreferences sp;
    private String root;
    private String taskName;
    private String dbPath;

    private DBManagerUtil dbManagerUtil;
    private String beizhu2 = "2图纸";
    private String dcqb;
    private String period;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paper_photo_add_list);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("样方列表");
    }

    @Override
    public void initView() {
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        taskName = getIntent().getStringExtra("taskName");
        dbPath = root + "/" + taskName + "/TASK/TRANSPORT/DATA.db";

        period = getIntent().getStringExtra("period");
        period = period.contains("早稻面积") ? "春播面积" : period;
        dcqb = period + "-" + "遥感面积调查表";
        dbManagerUtil = DBManagerUtil.getInstance(dbPath, this);
        getDatas();
        listview = (ListView) main.findViewById(R.id.photo_listView);
        if (adapter == null) {
            adapter = new TZPSListAdapter(this, dataList, node, 3);
            adapter.setOnOperateListener(listener);
            listview.setAdapter(adapter);
        }

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Constants.select = dataList.get(position);

                Intent intent = new Intent(YFPhotoActivity.this, YFTZPhotoActivity.class);
                intent.putExtra("from", 1);
                intent.putExtra("period", period);
                intent.putExtra("taskName", taskName);
                startActivity(intent);
                overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
            }
        });
    }

    TZPSListAdapter.OnOperateListener listener = new TZPSListAdapter.OnOperateListener() {

        @Override
        public void OnTake(int position) {

            Constants.select = dataList.get(position);

            Intent intent = new Intent(YFPhotoActivity.this, YFTZPhotoActivity.class);
            intent.putExtra("from", 1);
            intent.putExtra("period", period);
            intent.putExtra("taskName", taskName);
            startActivity(intent);
            overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        }

        @Override
        public void OnShow(int position) {

            Constants.select = dataList.get(position);

            Intent intent = new Intent(YFPhotoActivity.this, YFTZPhotoActivity.class);
            intent.putExtra("from", 1);
            intent.putExtra("period", period);
            intent.putExtra("taskName", taskName);
            startActivity(intent);
            overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        }
    };

    private Node node;

    private void getDatas() {
        // 取到配置文件中当前层级的节点
        node = Constants.nodelist.get(1);
        Check check = node.getCheck();


        // 获取数据库中当前层级的下级数据列表
        String[] fields = {node.getFeild(), node.getItem().getFeild(), "rowid"};
        try {
            DBImportUtil.openDatabase(root, Constants.currentTask.getTitle(), "/TASK/DATA.db");
            if (check == null) {
                dataList = DBImportUtil.listData(node.getTable(), fields, null, null);
            } else {
                String fld = check.getPfeild().toUpperCase();
                String value = Constants.tzps_parent.get(fld);
                dataList = DBImportUtil.listData(node.getTable(), fields, check.getFeild(), value);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "操作外部数据库出错", Toast.LENGTH_SHORT).show();
        }

        dodata();

    }

    private void dodata() {
        //处理数据
        for (Map<String, String> map : dataList) {
            String yfbhu = map.get("YFBHU");
            String where = " 样方编号 = '" + yfbhu + "' and DCQB = '" + dcqb + "' and TYPE = '3'";
            int c = dbManagerUtil.getTZPhotoCount("PHOTO", where, dbPath);
            map.put("flag", String.valueOf(c));
            System.out.println("样方：" + c);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();

        if (Constants.data_refresh) {
            dodata();
            adapter.notifyDataSetChanged();
        }

    }
}
